import router from './router'
import store from './store'
import { asyncRouterMap } from './router' // 引入权限路由和非权限路由
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)

  // determine whether the user has logged in
  const hasToken = getToken()

    if(hasToken) {
      if (to.path === '/login') {
        next()
  
        NProgress.done()
      } else {
        const hasGetUserRole = store.getters.roles
        if (hasGetUserRole) {
          next()
         
        } else {
          var role = ''
          if(localStorage.getItem('userInfo')) {
            role = JSON.parse(localStorage.getItem('userInfo')).role;
            store.commit('user/SET_ROLES', role)
          } else {
            role =  store.getters.userInfo.role
            
            store.commit('SET_ROLES',role)
          }
            if(role=='4') {
            // get user info
            // await store.dispatch('user/getInfo')
            // const roles=store.getters.roles;
            // const asyncRouterMap = localStorage.getItem('routerCache');
            // console.log(roles)
            // console.log(asyncRouterMap)
            //本地asyncRouterMap路由表
              // store.dispatch('permission/GenerateRoutes', { roles }).then(() => { // 生成可访问的路由表
              const arr = router.options.routes
              const OVERCONTENT = arr.concat(asyncRouterMap)
              router.options.routes = OVERCONTENT
              // console.log(store.getters.addRouters)
              router.addRoutes(asyncRouterMap); // 动态添加可访问路由表
              // router.options.routes=store.getters.routers;
              store.dispatch('user/setToken','21')
              next({ ...to, replace: true });// hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
            // })
  
            // next()
            } else {
              next()
  
            }
        }
      }
  } else {
      if(to.path=='/login') {
        next()
      } else {
        next(`/login?redirect=${to.path}`)
      }
      // if (whiteList.indexOf(to.path) !== -1) {
      //   // in the free login whitelist, go directly
      //   next()
      // } else {
      //   // other pages that do not have permission to access are redirected to the login page.
      //   if (to.path === '/register') {
      //     next()
      //   } else {
      //     next(`/login?redirect=${to.path}`)
      //   }
        NProgress.done()
      // }

    }
  // } else {
  //   /* has no token*/

  //   if (whiteList.indexOf(to.path) !== -1) {
  //     // in the free login whitelist, go directly
  //     next()
  //   } else {
  //     // other pages that do not have permission to access are redirected to the login page.
  //     next(`/login?redirect=${to.path}`)
  //     NProgress.done()
  //   }
  // }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
